﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace MyStream.UI
{
    public partial class frmMain : Form
    {
        private Options _options;
        public Options CurrentOptions
        {
            get { return _options; }
            set
            {
                _options = value;
                SaveOptions();
            }
        }

        public frmMain()
        {
            
            InitializeComponent();
            LoadNotify();
            LoadOptions();
        }

        private void LoadNotify()
        {
            this.Visible = false;
            this.Height = 0;
            this.Width = 0;
            this.Left = -300;
            this.Top = -300;

          
            this.userNotifiyIcon.ShowBalloonTip(3, "MyStream Started!!!", "Select Config to select workspace to audit", ToolTipIcon.Info);
        }

        private void ConfigMenu_Click(object sender, EventArgs e)
        {
            frmOptions config = new frmOptions(this);
            config.ShowDialog();
        }

        private void SaveOptions()
        {
            XmlSerializer ser = new XmlSerializer(typeof(Options));
            
            FileStream fs = new FileStream(@"options.xml",FileMode.Create);
            ser.Serialize(fs,_options);
        }

        private void StopMenu_Click(object sender, EventArgs e)
        {
            this.userNotifiyIcon.Visible = false;
            this.userNotifiyIcon.Dispose();
            this.Close();
            this.Dispose();
        }

        private void LoadOptions()
        {
            if (File.Exists("options.xml"))
            {
                XmlSerializer ser = new XmlSerializer(typeof(Options));
                FileStream fs = new FileStream(@"options.xml",FileMode.Open);
                _options = ser.Deserialize(fs) as Options;
            }
            else
            {
                _options = new Options();
            }

        }

        private void timShelve_Tick(object sender, EventArgs e)
        {
            int operation = 0;
            int pendingChanges = 0;

            foreach (WorkspaceInfo wsi in _options.WorkSpaces)
            {
                
                MyStream.Core.ShelveSetsInfos ssi = MyStream.Core.ShelveService.CreateShelveSet(wsi.workspaceName,wsi.serverName,
                    "MyStream_" + 
                    DateTime.Now.Year.ToString() + 
                    DateTime.Now.Month.ToString() + 
                    DateTime.Now.Day.ToString() + "_" +
                    DateTime.Now.Hour.ToString() + 
                    DateTime.Now.Minute.ToString() + "." + (operation + 1).ToString()
                );

                if (ssi.count > 0)
                {
                    operation++;
                    pendingChanges += ssi.pendingChangesCount;
                }

            }
            string msg = string.Format(
                    "{0} new(s) ShelveSet(s) created \r\n with {1} pending changes",
                    operation,
                    pendingChanges
                    );
            this.userNotifiyIcon.ShowBalloonTip(1500, "New ShelveSets", msg, ToolTipIcon.Info);

            if (_options != null && _options.MaxDuration > 0)
            {
                TimeSpan duration = new TimeSpan(0, _options.MaxDuration, 0);
                DateTime olderThan = DateTime.Now - duration;
                int deletedshelvesets = DeleteUseLessShelvesets(olderThan);
                msg = string.Format(
                    "{0} shelvesets deleted because they were created before : {1}",
                    deletedshelvesets,
                    olderThan.ToShortDateString() + " " + olderThan.ToShortTimeString()
                );
                if(deletedshelvesets > 0)
                    this.userNotifiyIcon.ShowBalloonTip(1500, "Deleted ShelveSets", msg, ToolTipIcon.Info);
            }
            

        }

        private void StartMenu_Click(object sender, EventArgs e)
        {
            this.timShelve.Interval = _options.Interval * 1000 * 60;
            this.timShelve.Enabled = true;
            this.timShelve.Start();
            
        }

        private int DeleteUseLessShelvesets(DateTime olderThan)
        {
             return MyStream.Core.ShelveService.DeleteOldShelveSets(olderThan, _options.CommentMarkPattern);
        }
    }
}
